﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Threading;
using Interface;
using Model;

namespace CC
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class CController : ICController
    {
        Random R;
        ClsPumpestationCol Pumpestationer;
        ClsPumpeCol Pumper;
        ClsKanalCol Kanaler;
        Thread t1;

        public CController()
        {
            this.R = new Random();
            this.Kanaler = new ClsKanalCol();
            this.Pumper = new ClsPumpeCol();
            this.Pumpestationer = new ClsPumpestationCol(Kanaler, Pumper);

            this.t1 = new Thread(SendVandstande);
        }

        public void Connect(int Nr)
        {
            this.Pumpestationer.TilføjPumpestation(Nr);

            if (this.t1.IsAlive == false)
            {
                this.t1.Start();
                this.Kanaler.Kanaler[0].Vandstand = this.Kanaler.Kanaler[0].kapacitet / 2;
            }
        }

        public void SendVandstande()
        {
            while (true)
            {
                Thread.Sleep(5000);
                foreach (ClsPumpestation P in this.Pumpestationer.Pumpestationer)
                {
                    foreach (ClsKanal K in P.Kanaler)
                    {
                        P.SendVandstand(K.Id);
                    }
                }
            }
        }

        public bool StartPumpe(int PumpeId)
        {
            Console.WriteLine("Pumpe " + PumpeId + " start");
            return this.Pumper.startPumpe(PumpeId);
        }

        public bool SlukPumpe(int PumpeId)
        {
            Console.WriteLine("Pumpe " + PumpeId + " sluk");
            return this.Pumper.SlukPumpe(PumpeId);
        }

        public void DefektPumpe(int PumpeId, bool Defekt)
        {
            this.Pumper.DefektPumpe(PumpeId, Defekt);
        }
    }
}
